use proc_macro::{TokenStream};
use syn::{DeriveInput};
use quote::{quote};

#[proc_macro_derive(RawData)]
pub fn derive(input: TokenStream) -> TokenStream {
	let st:DeriveInput = syn::parse(input).unwrap();
	let result = do_expand(st);
	match result {
		Ok(ts) => ts.into(),
		Err(e) => e.to_compile_error().into()
	}
	//unimplemented!()
}

fn do_expand(st:DeriveInput) -> syn::Result<TokenStream> {
	let struct_ident = &st.ident;
	Ok(quote!(
		impl #struct_ident {
			pub fn as_raw_data(&self) -> &[u8] {
				unsafe { std::mem::transmute::<&Self,&[u8;std::mem::size_of::<Self>()]>(self) }
			}
			pub fn as_raw_mut(&mut self) -> &mut [u8] {
				unsafe { std::mem::transmute::<&mut Self,&mut [u8;std::mem::size_of::<Self>()]>(self) }
			}
		}
	).into())
}

